# -*- coding: utf-8 -*-
from rest_framework.views import APIView
from .models import SchoolResidentialRel
from libs.response import *


class SearchSchoolView(APIView):
    """
    根据小区搜学校
    """
    def get(self, request, *args, **kwargs):
        """
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        result = JSONResponse.init_response_data()
        residential_name = request.query_params.get("residential_name")
        school_s = SchoolResidentialRel.objects.filter(
            residential_name__contains=residential_name)
        school_list = []
        for obj in school_s:
            school_list.append(obj.school_name)
        result["data"] = {"school_list": school_list}
        response = JSONResponse(result)
        return response


class SearchResidentialView(APIView):
    """
    根据学校搜小区
    """
    def get(self, request, *args, **kwargs):
        """
        :param request:
        :param args:
        :param kwargs:
        :return:
        """
        result = JSONResponse.init_response_data()
        school_name = request.query_params.get("school_name")
        residential_s = SchoolResidentialRel.objects.filter(
            school_name__contains=school_name)
        residential_list = []
        for obj in residential_s:
            residential_list.append(obj.residential_name)
        result["data"] = {"residential_list": residential_list}
        response = JSONResponse(result)
        return response
